#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
    #include "wx/mdi.h"
#endif

#include "wx/toolbar.h"
#include "wx/grid.h"
#include "wx/dialog.h"
#include "wx/spinctrl.h"
#include "wx/stattext.h"
#include "wx/spinbutt.h"

#include <sqlite3.h>
#include <sqlite3util.h>
#include "dbi.h"
#include "mdi.h"
#include <Configuration.h>

//linux header include
#include <sys/types.h>
#include <signal.h>
#include <iostream>
#include <vector>

extern ConfigurationTable gConfig;
extern MainFrame *gPtrframe;
extern void CPData_modifier();
extern void CPData_modfy2();
extern void sendmail();

BEGIN_EVENT_TABLE(WelcomeMsgModalDialog, wxDialog)
    EVT_BUTTON(wxID_ANY, WelcomeMsgModalDialog::OnButton)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(AddingWelcomeMsgModalDialog, wxDialog)
    EVT_BUTTON(wxID_ANY, AddingWelcomeMsgModalDialog::OnButton)
END_EVENT_TABLE()

WelcomeMsgModalDialog::WelcomeMsgModalDialog(wxWindow *parent):
    wxDialog(parent, 
        wxID_ANY, 
        wxT(""), 
        wxDefaultPosition, 
        wxDefaultSize, 
        wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
    wxBoxSizer *sizerTop     = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *sizerHeader  = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *sizerContent = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *sizerButtom  = new wxBoxSizer(wxHORIZONTAL);
    wxArrayString choices;
    wxString      wxStrHint(_("Choosing item ..."));
    choices.Add(wxStrHint);
    
    sizerHeader->Add(
        new wxStaticText(
            this, 
            wxID_STATIC, 
            _("Welcome Message :"), 
            wxDefaultPosition, 
            wxDefaultSize, 
            wxALIGN_LEFT),
        1,
        wxALIGN_LEFT | wxALIGN_BOTTOM, 
        5);        
    
    getData(choices);
    /*
    m_pMsgChoice = new wxChoice(this, 
        wxID_ANY,
        wxDefaultPosition,
        wxDefaultSize,
        choices);
    sizerHeader->Add(
        m_pMsgChoice,
        0,
        wxALIGN_LEFT | wxALIGN_BOTTOM, 
        5);
    */
    //std::string strVal = gConfig.getStr("Control.LUR.OpenRegistration.Message");
    SMSRecord rec;
    gConfig.getWELMSG(rec);
    std::string strVal;
    if (rec.msg_len == 0) {
        strVal = "no welcome msg.";
    }
    else {
        strVal.append(rec.msg_utf8);
    }
    
    wxString wxVal(strVal.c_str(), wxConvUTF8);
    m_pTxtMsgContent = new wxTextCtrl(this,
        wxID_ANY,
        wxT(" "),
        wxDefaultPosition,
        wxDefaultSize,
        wxTE_LEFT);
    m_pTxtMsgContent->SetValue(wxVal);
    sizerContent->Add(
        m_pTxtMsgContent,
        1,
        wxALIGN_LEFT|wxALIGN_RIGHT|wxEXPAND | wxALL,
        5);
    m_pBtnDel        = new wxButton(this, wxID_ANY, _("Delete"));
    m_pBtnDel->Enable(false);
    m_pBtnSetDefault = new wxButton(this, wxID_ANY, _("Set Default"));
    m_pBtnAddNew     = new wxButton(this, wxID_ANY, _("Add New..."));
    m_pBtnAddNew->Enable(false);
    m_pBtnCancel     = new wxButton(this, wxID_ANY, _("Cancel"));
    sizerButtom->Add(m_pBtnDel, 0, wxALIGN_CENTER | wxALL, 5);
    sizerButtom->Add(m_pBtnSetDefault, 0, wxALIGN_CENTER | wxALL, 5);
    sizerButtom->Add(m_pBtnAddNew, 0, wxALIGN_CENTER | wxALL, 5);
    sizerButtom->Add(m_pBtnCancel, 0, wxALIGN_CENTER | wxALL, 5);
    sizerTop->Add(sizerHeader, 0, wxALIGN_LEFT);
    sizerTop->Add(sizerContent, 1, wxALIGN_CENTER | wxEXPAND);
    sizerTop->Add(sizerButtom, 0, wxALIGN_CENTER);
    SetSizer(sizerTop);
    sizerTop->Fit(this);
    sizerTop->SetSizeHints(this);
}

void WelcomeMsgModalDialog::getData(wxArrayString& choices)
{
    
}

void WelcomeMsgModalDialog::OnButton(wxCommandEvent& evt)
{
    if (evt.GetEventObject() == m_pBtnCancel)
    {
        Close();
    }
    else if (evt.GetEventObject() == m_pBtnAddNew)
    {
        /*AddingWelcomeMsgModalDialog dlg(this);
        dlg.CentreOnParent();
        dlg.ShowModal();*/
        //sendmail();
    }
    else if (evt.GetEventObject() == m_pBtnSetDefault)
    {
        wxString wxVal = m_pTxtMsgContent->GetValue();
        std::string strVal(wxVal.mb_str());
        //gConfig.set("Control.LUR.OpenRegistration.Message", strVal);
        gConfig.setWelmsg(strVal);
        //strVal = gConfig.getStr("Control.LUR.OpenRegistration.Message");
        //wxVal = wxString(strVal.c_str(), wxConvUTF8);
        //m_pTxtMsgContent->SetValue(wxVal);
        Close();
    }
    else 
    {
        evt.Skip();
    }
}

AddingWelcomeMsgModalDialog::AddingWelcomeMsgModalDialog(wxWindow *parent)
    :wxDialog(parent, 
        wxID_ANY, 
        wxT(" "), 
        wxDefaultPosition, 
        wxDefaultSize, 
        wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
    wxBoxSizer *sizerTop     = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *sizerContent = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *sizerAging   = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *sizerBottom  = new wxBoxSizer(wxHORIZONTAL);
    
    sizerContent->Add(
        new wxStaticText(
            this, 
            wxID_STATIC, 
            _("Content:"), 
            wxDefaultPosition, 
            wxDefaultSize, 
            wxALIGN_LEFT),
        1,
        wxALIGN_LEFT | wxALIGN_BOTTOM, 
        5);        
    m_pWelcomeMsgContent = new wxTextCtrl(this,
        wxID_ANY,
        wxT(" "),
        wxDefaultPosition,
        wxDefaultSize,
        wxTE_MULTILINE | wxTE_LEFT);
    sizerContent->Add(
        m_pWelcomeMsgContent,
        0, 
        wxALIGN_LEFT | wxALIGN_RIGHT | wxALIGN_BOTTOM | wxEXPAND,
        5);
    
    sizerAging->Add(
        new wxStaticText(
            this, 
            wxID_STATIC, 
            _("Aging: "), 
            wxDefaultPosition, 
            wxDefaultSize, 
            wxALIGN_LEFT),
        1,
        wxALIGN_LEFT | wxALIGN_BOTTOM,
        5);
    m_pSpinAging = new wxSpinCtrl(
        this, 
        wxID_ANY, 
        wxEmptyString,
        wxDefaultPosition, 
        wxDefaultSize, 
        wxSP_ARROW_KEYS,
        1, MAX_DURATION_DAYS, 1,
        wxT("wxSpingCtrl"));
    sizerAging->Add(
        m_pSpinAging,
        1,
        wxALIGN_LEFT | wxALIGN_BOTTOM,
        5);
    m_pBtnAdd = new wxButton(this, wxID_ANY, _("Add"));
    m_pBtnCancel = new wxButton(this, wxID_ANY, _("Cancel"));
    sizerBottom->Add(m_pBtnAdd, 1, wxALIGN_CENTER | wxALL, 5);
    sizerBottom->Add(m_pBtnCancel, 1, wxALIGN_CENTER | wxALL, 5);
    sizerTop->Add(sizerContent, 0, wxALIGN_LEFT | wxEXPAND |wxALL, 5);
    sizerTop->Add(sizerAging, 1, wxALIGN_LEFT | wxALL, 5);
    sizerTop->Add(sizerBottom, 1, wxALIGN_CENTER | wxALL, 5);
    
    SetSizer(sizerTop);
    sizerTop->Fit(this);
    sizerTop->SetSizeHints(this);
}

void AddingWelcomeMsgModalDialog::OnButton(wxCommandEvent& evt)
{
    if (evt.GetEventObject() == m_pBtnCancel)
    {
        Close();
    }
    else 
    {
        evt.Skip();
    }
}
